﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using pe.edu.pucp.lp2.controller;
using SalesModel;
using SalesController;

namespace SalesApp
{
    public partial class ClienteForm : Form
    {
        public ClienteForm()
        {
            InitializeComponent();
            String[] installs = new String[] { "Activo", "No Activo"};

            cmbStatus.Items.AddRange(installs);
        }

        public void RefreshGrid()
        {
            dgvCustomer.Rows.Clear();

            List<Customer> list = SalesManager.getAllCustomers();

            for (int i = 0; i < list.Count; i++)
            {
                dgvCustomer.Rows.Add(new String [] { ""+list[i].getId_customer(),
                                                         list[i].getDni(),
                                                         list[i].getName(),
                                                         list[i].getmLastName(),
                                                         list[i].getfLastName(),
                                                         ""+list[i].getRegDate(),
                                                         ""+list[i].getStatus()
                });
            }
        }

        private void ClienteForm_Load(object sender, EventArgs e)
        {
            // TODO: esta línea de código carga datos en la tabla 'inf282DataSet.BoolCustomer' Puede moverla o quitarla según sea necesario.
            //this.boolCustomerTableAdapter.Fill(this.inf282DataSet.BoolCustomer);
            RefreshGrid();
        }

        private void dgvCustomer_Click(object sender, EventArgs e)
        {
            int selRow = dgvCustomer.SelectedRows[0].Index;
            int custId = Int32.Parse(dgvCustomer.Rows[selRow].Cells[0].Value.ToString());
            Customer c = SalesManager.getCustomerById(custId);

            txtId.Text = "" + c.getId_customer();
            txtName.Text = c.getName();
            txtmLastName.Text = c.getfLastName();
            txtpLastName.Text = c.getmLastName();
            txtPhone.Text = c.getPhoneNumber();
            txtregDate.Text = "" + c.getRegDate();
            if (c.getStatus() == 1)
                cmbStatus.SelectedItem = "Activo";
            else
                cmbStatus.SelectedItem = "No Activo";
            txtDNI.Text = c.getDni();
            txtEmail.Text = c.getEmail();
            txtAddress.Text = c.getAddress();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtId.Text);
            String name = txtName.Text;
            String fLastName = txtmLastName.Text;
            String mLastName = txtpLastName.Text;
            String address = txtAddress.Text;
            String email = txtEmail.Text;
            String phone = txtPhone.Text;
            DateTime regDate = DateTime.Parse(txtregDate.Text);
            int status = 0;
            if (cmbStatus.SelectedItem == "Activo")
                status = 1;
            else
                status = 0;
            String dni = txtDNI.Text;           

            Customer c = new Customer();

            c.setId_customer(id);
            c.setName(name);
            c.setmLastName(mLastName);
            c.setfLastName(fLastName);
            c.setAddress(address);
            c.setPhoneNumber(phone);
            c.setEmail(email);
            c.setRegDate(regDate);
            c.setStatus(status);
            c.setDni(dni);

            SalesManager.addCustomer(c);
            RefreshGrid();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtId.Text);
            String name = txtName.Text;
            String fLastName = txtmLastName.Text;
            String mLastName = txtpLastName.Text;
            String address = txtAddress.Text;
            String email = txtEmail.Text;
            String phone = txtPhone.Text;
            DateTime regDate = DateTime.Parse(txtregDate.Text);
            int status = 0;
            if (cmbStatus.SelectedItem == "Activo")
                status = 1;
            else
                status = 0;
            String dni = txtDNI.Text;

            Customer c = new Customer();

            c.setId_customer(id);
            c.setName(name);
            c.setmLastName(mLastName);
            c.setfLastName(fLastName);
            c.setAddress(address);
            c.setPhoneNumber(phone);
            c.setEmail(email);
            c.setRegDate(regDate);
            c.setStatus(status);
            c.setDni(dni);

            SalesManager.updateCustomer(c);
            this.boolCustomerTableAdapter.Update(this.inf282DataSet.BoolCustomer);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            String dni = txtDNI.Text;
            SalesManager.deleteCustomer(dni);
            RefreshGrid();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtId.Text = "";
            txtName.Text = "";
            txtpLastName.Text = "";
            txtmLastName.Text = "";
            txtPhone.Text = "";
            txtregDate.Text = "" + dt;
            txtDNI.Text = "";
            txtEmail.Text = "";
            txtAddress.Text = "";
        }
    }
}
